package com.xbao.service.impl;

import com.github.pagehelper.PageHelper;
import com.xbao.core.http.CommRequest;
import com.xbao.core.util.MyBeanUtil;
import com.xbao.core.util.pub.tool.StringUtils;
import com.xbao.dao.RoomMapper;
import com.xbao.model.Room;
import com.xbao.model.RoomExample;
import com.xbao.model.req.RoomReq;
import com.xbao.model.vo.RoomVo;
import com.xbao.service.RoomService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by qingshengzheng on 18/8/28.
 */
@Service
public class RoomServiceImpl implements RoomService {
    @Resource
    private RoomMapper roomMapper;

    @Override
    public void addRoom(RoomReq roomReq) {
        roomReq.setCreatetime(System.currentTimeMillis());
        roomReq.setUpdatetime(System.currentTimeMillis());
        roomMapper.insert(roomReq);

    }

    @Override
    public void deleteRoom(Long id) {
        roomMapper.deleteByPrimaryKey(id);

    }

    @Override
    public RoomVo detailRoom(Long id) {
        Room room = roomMapper.selectByPrimaryKey(id);
        if (room == null) return null;
        RoomVo vo = MyBeanUtil.copyProperties(room, RoomVo.class);
        return vo;

    }

    @Override
    public void modifyRoom(RoomReq req) {
        roomMapper.updateByPrimaryKeySelective(req);

    }

    @Override
    public List<RoomVo> queryRooms(RoomReq req) {
        if(req==null) req=new RoomReq();
        RoomExample example = new RoomExample();
        RoomExample.Criteria criteria = example.createCriteria();
        if (req.getPager() != null) {
            PageHelper.startPage(req.getPager().getPageNo(), req.getPager().getPageSize());
            example.setOrderByClause("id desc");
        }
        if (req.getId() != null)
            criteria.andIdEqualTo(req.getId());
        if(!StringUtils.isNull(req.getName()))
            criteria.andNameLike("%"+req.getName()+"%");
        if (req.getLimit() != null)
            example.setLimit(req.getLimit());
        List<Room> rooms = roomMapper.selectByExample(example);
        List<RoomVo> vos = MyBeanUtil.copyList(rooms, RoomVo.class);
        return vos;
    }

    /**
     * @param orgid
     * @return
     */
    @Override
    public Integer countRoomsOfOrg(Long orgid) {
     RoomExample example=new RoomExample();
     RoomExample.Criteria criteria=example.createCriteria();
     criteria.andOrgidEqualTo(orgid);
    Integer count= roomMapper.countByExample(example);
        return count;






    }

    @Override
    public List<RoomVo> allRooms(Long orgid) {
       RoomReq req=new RoomReq();
       return queryRooms(req);

    }
}
